//
//  ModifyPwdViewController.m
//  moon
//
//  Created by beyself on 16/6/15.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import "Utils.h"
#import "ModifyPwdViewController.h"
#import "MBProgressHUD.h"

@interface ModifyPwdViewController ()

@end

@implementation ModifyPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"修改"
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(modifyPwd)];
    int yPos = 10;
    for(int i = 0; i < 4; i++)
    {
        UIView* subview = [[UIView alloc] init];
        subview.frame = CGRectMake(0, yPos, SCREEN_WIDTH, 48);
        subview.backgroundColor = WHITE;
        [self.view addSubview:subview];
        
        UILabel* label = [[UILabel alloc] init];
        label.frame = CGRectMake(10, 0, 80, 48);
        [self.view addSubview:label];
        label.font = FONT_14;
        label.textColor = GRAY;
        [subview addSubview:label];
        
        UITextField* textfield = [[UITextField alloc] init];
        textfield.frame = CGRectMake(80, 0, SCREEN_WIDTH - 90, 48);
        textfield.font = FONT_14;
        textfield.textColor = BLACK;
        textfield.tag = 2000 + i;
        [subview addSubview:textfield];
        
        if(i == 0)
        {
            label.text = @"姓名:";
            yPos += 58;
            textfield.text = _name;
            textfield.enabled = NO;
        }
        else if(i == 1)
        {
            textfield.secureTextEntry = YES;
            label.text = @"原密码:";
            yPos += 48;
        }
        else if(i == 2)
        {
            textfield.secureTextEntry = YES;
            label.text = @"新密码:";
            yPos += 48;
        }
        else if(i == 3)
        {
            textfield.secureTextEntry = YES;
            label.text = @"确认密码:";
            yPos += 48;
        }
        
        CALayer* layer = [[CALayer alloc] init];
        layer.frame = CGRectMake(0, 47.5, SCREEN_WIDTH, .5);
        layer.backgroundColor = BG.CGColor;
        [subview.layer addSublayer:layer];
    }
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:singleTapGestureRecognizer];
}

- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer
{
    UITextField* old = ((UITextField*)[self.view viewWithTag:2001]);
    UITextField* newPwd0 = ((UITextField*)[self.view viewWithTag:2002]);
    UITextField* newPwd1 = ((UITextField*)[self.view viewWithTag:2003]);
    
    [old resignFirstResponder];
    [newPwd0 resignFirstResponder];
    [newPwd1 resignFirstResponder];
}

- (void) modifyPwd
{
    NSString* old = ((UITextField*)[self.view viewWithTag:2001]).text;
    NSString* newPwd0 = ((UITextField*)[self.view viewWithTag:2002]).text;
    NSString* newPwd1 = ((UITextField*)[self.view viewWithTag:2003]).text;
    
    if(old.length == 0 || newPwd0.length == 0 || newPwd1.length == 0)
    {
        [self showAllTextDialog:@"请完善所有信息"];
        return;
    }
    
    NSString* save = [Utils readDefaultUser:@"password"];
    if(![save isEqualToString:old])
    {
        [self showAllTextDialog:@"旧密码不正确"];
        return;
    }
    
    if(![newPwd0 isEqualToString:newPwd1])
    {
        [self showAllTextDialog:@"两次密码不一致"];
        return;
    }
    
    [(UITextField*)[self.view viewWithTag:2001] resignFirstResponder];
    [(UITextField*)[self.view viewWithTag:2002] resignFirstResponder];
    [(UITextField*)[self.view viewWithTag:2003] resignFirstResponder];
    
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setValue:newPwd1 forKey:@"password"];
    [param setValue:APPDELEGATE.mMemberInfo[@"id"] forKey:@"id"];
    [Utils sendHttpPost:param method:@"update_profile" controller:self flag:0];
}

- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    NSString* newPwd1 = ((UITextField*)[self.view viewWithTag:2002]).text;
    [Utils saveDefaultUser:newPwd1 key:@"password"];
    
    [self showAllTextDialog:@"修改成功"];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
